package com.hmdp.listener;

import com.hmdp.entity.VoucherOrder;
import com.hmdp.service.impl.VoucherOrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.hmdp.service.impl.VoucherOrderServiceImpl.SECKILL_ORDER_CLOSE_DELAY_TOPIC;

/**
 * 未支付订单过期自动消费
 */
@Slf4j
@Component
@RocketMQMessageListener(
        topic = SECKILL_ORDER_CLOSE_DELAY_TOPIC, // 监听的主题
        consumerGroup = "seckill_order_close_consumer_group", // 消费者组（读取配置）
        consumeMode = ConsumeMode.CONCURRENTLY,
        consumeThreadNumber = 40
)
public class SeskillDelayListener implements RocketMQListener<VoucherOrder>{

    @Autowired
    private VoucherOrderServiceImpl voucherOrderService;

    @Override
    public void onMessage(VoucherOrder voucherOrder) {
        try {
            //关单
            voucherOrderService.handleCloseOrder(voucherOrder);
            log.info("RocketMQ消费成功，订单id：{}", voucherOrder.getId());
        } catch (Exception e) {
            log.error("RocketMQ消费失败，订单id：{}", voucherOrder.getId(), e);
            // 抛出异常，RocketMQ会自动重试（默认重试16次，可配置）
            throw new RuntimeException("订单处理失败，触发RocketMQ重试", e);
        }

    }


}
